package com.ruubypay.biz.utils.http;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * Desc : Http请求工具类
 * @author sucyun 
 * Date :2018/4/7 上午11:29
 */
public class HttpMethodUtil {
    volatile static int s = 0;
    public static void main(String[] args) {
        s = 3;
//        Map<String,String> map = new HashMap<String,String>();
//        map.put("startTime","1500055082");
//        map.put("endTime","1519955082");
//        map.put("limit","300");
//        //发送 POST 请求
//        //startTime=1500055082&endTime=1519955082&limit=300
//        String sr = HttpMethodUtil.httpPostRequest("http://172.16.60.98:8080/api/v1/alarm/eventcases","startTime=1500055082&endTime=1519955082&limit=300");
//        System.out.println(sr);
    }

    /**
     * Desc : 默认json post请求
     * @author sucyun
     * @param url 请求路径
     * @param entity 请求entity
     * @param param 请求参数
     * @return 请求结果
     * Date :2018/4/7 上午11:20
     */
    public static String httpPostRequest(String url,StringEntity entity, String param) {
        HttpPost post = new HttpPost(url);
        post.setEntity(entity);
        return doRequest(post);
    }
    /**
     * Desc : 默认json post请求
     * @author sucyun
     * @param url 请求路径
     * @param param 请求参数
     * @return 请求结果
     * Date :2018/4/7 上午11:20
     */
    public static String httpPostRequest(String url, String param) {
        HttpPost post = new HttpPost(url);
        StringEntity entity = new StringEntity(param,"utf-8");
        entity.setContentType("application/json");
        post.setEntity(entity);
        return doRequest(post);
    }
    /**
     * Desc : json Post请求
     * @author sucyun
     * @param url 请求路径
     * @param param 请求参数
     * @return 请求结果
     * Date :2018/4/7 上午11:20
     */
    public static String httpJsonPostRequest(String url, String param) {
        CloseableHttpClient client = HttpClients.createDefault();
        CloseableHttpResponse response;
        InputStream iStream = null;
        String res = null;
        HttpPost post = new HttpPost(url);
        try {
            StringEntity entity = new StringEntity(param,"utf-8");
            entity.setContentType("application/json");
            response = client.execute(post);
            HttpEntity responseEntity = response.getEntity();
            if(responseEntity != null){
                res = EntityUtils.toString(responseEntity,"utf-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            post.releaseConnection();
        }
        return res;
    }
    /**
     * Desc : Post请求
     * @author sucyun
     * @param post 请求Post 包含 header entity 等
     * @return 请求结果
     * Date :2018/4/7 上午11:20
     */
    public static String httpPostRequest(HttpPost post) {
        return doRequest(post);
    }
    /**
     * Desc : Get请求
     * @author sucyun
     * @param url 请求路径
     * @param param 请求参数
     * @return 请求结果
     * Date :2018/4/7 上午11:20
     */
    public static String httpGetRequest(String url, String param) {
        HttpGet get = new HttpGet(url + param);
        return doRequest(get);
    }
    /**
     * Desc : 文件或图片下载
     * @author sucyun
     * @param url 请求路径
     * @param param 请求参数
     * Date :2018/4/7 上午11:20
     */
    public static void httpFileGetRequest(String url, String param, HttpServletResponse httpResponse) {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpGet get = new HttpGet(url + param);
        CloseableHttpResponse response;
        OutputStream os = null;
        InputStream iStream = null;
        try {
            response = client.execute(get);
            HttpEntity responsEntity = response.getEntity();
            iStream = responsEntity.getContent();
            BufferedInputStream bufferedInputStream = new BufferedInputStream(iStream);
            os = httpResponse.getOutputStream();
            byte[] b = new byte[1024];
            while (bufferedInputStream.read(b) != -1) {
                os.write(b);
            }
            bufferedInputStream.close();
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            get.releaseConnection();
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (iStream != null) {
                try {
                    iStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * Desc : 实际请求
     * @author sucyun 
     * @param httpMethod 请求方法
     * @return 返回请求结果字符串
     * Date :2018/4/7 上午11:25
     */
    private static String doRequest(HttpRequestBase httpMethod){
        CloseableHttpClient client = HttpClients.createDefault();
        CloseableHttpResponse response;
        InputStream iStream = null;
        String res = null;
        try {
            response = client.execute(httpMethod);
            HttpEntity responseEntity = response.getEntity();
            iStream = responseEntity.getContent();
            InputStreamReader inputStreamReader = new InputStreamReader(iStream);
            BufferedReader reader = new BufferedReader(inputStreamReader);
            StringBuilder sb = new StringBuilder();
            while (reader.readLine() != null) {
                sb.append(reader.readLine());
            }
            reader.close();
            res = sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            httpMethod.releaseConnection();
            if (iStream != null) {
                try {
                    iStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return res;
    }

}

